﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using Agility.Wcf.ClientProxy;
using Agility.Wcf.ClientProxy.RequirementsManagementServiceClient;

namespace Agility.Office.ExcelAddIn
{
    public partial class RibbonAgility
    {
        private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        private AgilityClientProxy proxy;

        private void RibbonAgility_Load(object sender, RibbonUIEventArgs e)
        {
            logger.Debug("--> Entering RibbonAgility_Load");
            proxy = AgilityClientProxy.GetInstance(new Uri(Properties.Settings.Default.AgilityUrl));
            proxy.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(proxy_PropertyChanged);
            this.buttonLogin.Enabled = !proxy.IsLoggedOn;
            this.buttonGetMyIdeas.Enabled = proxy.IsLoggedOn;
            logger.Trace("LoginButton is {0}", this.buttonLogin.Enabled == true ? "enabled" : "disabled");
            proxy.KeepMeLoggedOn = Properties.Settings.Default.KeepMeLoggedOn;
            logger.Debug("<-- Leaving RibbonAgility_Load");
        }

        void proxy_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            this.logger.Debug("--> Entering proxy_PropteryChanged event handler");
            this.logger.Trace("Property is {0}", e.PropertyName);
            switch (e.PropertyName)
            {
                case "IsLoggedOn":
                {
                    this.buttonLogin.Enabled = !proxy.IsLoggedOn;
                    logger.Debug("Changed enabled state of Logon button to {0}", !proxy.IsLoggedOn);
                    this.buttonGetMyIdeas.Enabled = proxy.IsLoggedOn;
                    break;
                }
                default:
                    break;
            }
            this.logger.Debug("<-- Leaving proxy_PropteryChanged event handler");
        }

        private void groupLogin_DialogLauncherClick(object sender, RibbonControlEventArgs e)
        {
            this.logger.Debug("--> Entering groupLogin_DialogLauncherClick");
            AgilitySettingsDialog settingsDialog = new AgilitySettingsDialog();
            settingsDialog.ShowIcon = false;
            settingsDialog.ShowInTaskbar = false;
            settingsDialog.Tag = proxy;
            settingsDialog.ShowDialog();
            this.logger.Debug("<-- Leaving groupLogin_DialogLauncherClick");
        }

        private void buttonLogin_Click(object sender, RibbonControlEventArgs e)
        {
            this.logger.Debug("--> Entering buttonLogin_Click");
            try
            {
                AgilitySettingsDialog dlg = new AgilitySettingsDialog();
                dlg.ShowIcon = false;
                dlg.ShowInTaskbar = false;
                logger.Debug("Asking the user to populate the settings");
                System.Windows.Forms.DialogResult result = dlg.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    this.proxy.LogOn(dlg.UserName, dlg.Password);
                }
            }
            catch (ArgumentNullException)
            {
                logger.Warn("No user information present");
                AgilitySettingsDialog dlg = new AgilitySettingsDialog();
                dlg.ShowIcon = false;
                dlg.ShowInTaskbar = false;
                logger.Debug("Asking the user to populate the settings");
                dlg.ShowDialog();
                this.proxy.LogOn(dlg.UserName, dlg.Password);
            }
            finally
            {
                this.buttonLogin.Enabled = !this.proxy.IsLoggedOn;
                logger.Debug("Log on with user {0} {1}", Properties.Settings.Default.UserName, this.proxy.IsLoggedOn == true ? "successful" : "failed");
            }
            this.logger.Debug("<-- Leaving buttonLogin_Click");
        }

        private void buttonGetMyIdeas_Click(object sender, RibbonControlEventArgs e)
        {
            this.logger.Debug("--> Entering buttonGetMyIdeas_Click");
            List<Epic> ideas;
            try
            {
                ideas = this.proxy.GetMyIdeas();
                Globals.ThisAddIn.AddTableMyIdeas(ideas);
            }
            catch (Exception ex)
            {
                logger.Fatal(ex.ToString());
                throw;
            }
            this.logger.Debug("<-- Leaving buttonGetMyIdeas_Click");
        }

        private void buttonPublish_Click(object sender, RibbonControlEventArgs e)
        {
            this.logger.Debug("--> Entering buttonPublish_Click");
            try
            {
                Globals.ThisAddIn.PublishChanges();
            }
            catch (Exception ex)
            {
                this.logger.Fatal(ex.ToString());
                throw;
            }
            this.logger.Debug("<-- Leaving buttonPublish_Click");
        }
    }
}
